package com.hermit.qmst.UI.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hermit.qmst.R;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Preferences;

/**
 * Created by hermithermit on 15/2/18.
 */
public class DialSetActivity extends Activity implements View.OnClickListener{

    private Preferences mPreferences;
    private TextView mtvAutoring;
    private TextView mtvCallWaitRing;
    private TextView mtvDialKeyTone;
    private TextView mtvDialKeyVibrate;

    private TextView mtvCallback;
    private TextView mtvSipCall;
    private TextView mtvZNCall;
    private TextView mtvSDCall;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_dial_set);
        mPreferences = new Preferences(this);
        initView();
    }

    private void initView(){
        ((TextView)findViewById(R.id.tv_title)).setText("拨打设置");
        ((ImageView)findViewById(R.id.iv_title_left)).setImageResource(R.drawable.ic_back);
        findViewById(R.id.iv_title_left).setOnClickListener(this);

        mtvAutoring = (TextView) findViewById(R.id.rlyt_autoring);
        mtvAutoring.setOnClickListener(this);
        if (mPreferences.getPreferenceBooleanValue(Preferences.AUTO_RING)){
            mtvAutoring.setSelected(true);
        }else {
            mtvAutoring.setSelected(false);
        }

        mtvCallWaitRing = (TextView)findViewById(R.id.rlyt_media);
        mtvCallWaitRing.setSelected(mPreferences.getPreferenceBooleanValue(Preferences.CALL_WAIT_RING));
        mtvCallWaitRing.setOnClickListener(this);
        mtvDialKeyTone = (TextView)findViewById(R.id.rlyt_video);
        mtvDialKeyTone.setSelected(mPreferences.getPreferenceBooleanValue(Preferences.DIAL_KEY_TONE));
        mtvDialKeyTone.setOnClickListener(this);
        mtvDialKeyVibrate = (TextView)findViewById(R.id.rlyt_viber);
        mtvDialKeyVibrate.setSelected(mPreferences.getPreferenceBooleanValue(Preferences.DIAL_KEY_VIBRATE));
        mtvDialKeyVibrate.setOnClickListener(this);

        mtvCallback = (TextView)findViewById(R.id.rlyt_callback);
        mtvCallback.setOnClickListener(this);
        mtvSipCall = (TextView)findViewById(R.id.rlyt_sipcall);
        mtvSipCall.setOnClickListener(this);
        mtvZNCall = (TextView)findViewById(R.id.rlyt_zncall);
        mtvZNCall.setOnClickListener(this);
        mtvSDCall = (TextView)findViewById(R.id.rlyt_sdcall);
        mtvSDCall.setOnClickListener(this);

        setCallWay();
    }

    private void setCallWay(){

        String type = mPreferences.getPreferenceStringValue(Preferences.CALL_WAY);
        mtvZNCall.setSelected(false);
        mtvSipCall.setSelected(false);
        mtvCallback.setSelected(false);
        mtvSDCall.setSelected(false);
        if (type.equals("0")){
            mtvCallback.setSelected(true);
        }else if (type.equals("1")){
            mtvSipCall.setSelected(true);
        }else if (type.equals("2")){
            mtvZNCall.setSelected(true);
        }else if (type.equals("3")){
            mtvSDCall.setSelected(true);
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.iv_title_left:
                DialSetActivity.this.finish();
                break;
            case R.id.rlyt_autoring:

                if (mPreferences.getPreferenceBooleanValue(Preferences.AUTO_RING)){
                    mPreferences.setPreferenceBooleanValue(Preferences.AUTO_RING, false);
                    Common.mIsAutoAnswer = false;
                    mtvAutoring.setSelected(false);
                }else {
                    mPreferences.setPreferenceBooleanValue(Preferences.AUTO_RING, true);
                    Common.mIsAutoAnswer = true;
                    mtvAutoring.setSelected(true);
                }

                break;

            case R.id.rlyt_media:

                mtvCallWaitRing.setSelected(!mPreferences.getPreferenceBooleanValue(Preferences.CALL_WAIT_RING));
                mPreferences.setPreferenceBooleanValue(Preferences.CALL_WAIT_RING,!mPreferences.getPreferenceBooleanValue(Preferences.CALL_WAIT_RING));
                break;

            case R.id.rlyt_video:
                mtvDialKeyTone.setSelected(!mPreferences.getPreferenceBooleanValue(Preferences.DIAL_KEY_TONE));
                mPreferences.setPreferenceBooleanValue(Preferences.DIAL_KEY_TONE,!mPreferences.getPreferenceBooleanValue(Preferences.DIAL_KEY_TONE));
                break;
            case R.id.rlyt_viber:

                mtvDialKeyVibrate.setSelected(!mPreferences.getPreferenceBooleanValue(Preferences.DIAL_KEY_VIBRATE));
                mPreferences.setPreferenceBooleanValue(Preferences.DIAL_KEY_VIBRATE,!mPreferences.getPreferenceBooleanValue(Preferences.DIAL_KEY_VIBRATE));
                break;

            case R.id.rlyt_callback:

                mPreferences.setPreferenceStringValue(Preferences.CALL_WAY,"0");
                setCallWay();

                break;
            case R.id.rlyt_sipcall:

                mPreferences.setPreferenceStringValue(Preferences.CALL_WAY,"1");
                setCallWay();
                break;
            case R.id.rlyt_zncall:

                mPreferences.setPreferenceStringValue(Preferences.CALL_WAY,"2");
                setCallWay();

                break;
            case R.id.rlyt_sdcall:

                mPreferences.setPreferenceStringValue(Preferences.CALL_WAY,"3");
                setCallWay();

                break;
            default:
                break;
        }

    }
}
